﻿using System;
using Habanero.Base;
using Habanero.BO;
using Habanero.UI.Base;
using Habanero.UI.Win;
using UseCaseManager.BO;

namespace UseCaseManager.UI
{
    /// <summary>
    /// A parent-child control used for managing a Project, its Releases, Drops and UseCases
    /// </summary>
    public class DropCompletionControl : UserControlWin, IFormControl
    {
        private readonly IControlFactory _controlFactory;
        private BusinessObjectCollection<Drop> _dropCol;

       // public ReleasesGridControl ReleaseSelector { private set; get; }

        /// <summary>
        /// The Selector used to Select the Drop that the Use cases are being selected for.
        /// </summary>
        public DropsGridControlShowsRelease DropSelector { private set; get; }
        
        /// <summary>
        /// A multi selector showing the Use cases selected for the drop and the Use Cases available for selection.
        /// </summary>
        public UseCaseCompletedSelectorControl UseCaseCompletedControl { get; private set; }

        /// <summary>
        /// Constructor for the <see cref="DropCompletionControl"/>
        /// </summary>
        public DropCompletionControl()
        {
            _controlFactory = GlobalUIRegistry.ControlFactory;
            //ReleaseSelector=new ReleasesGridControl(new ControlFactoryWin());
            DropSelector = new DropsGridControlShowsRelease(new ControlFactoryWin());
            UseCaseCompletedControl = new UseCaseCompletedSelectorControl();

            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(this);
            DropSelector.Width = 350;
            //layoutManager.AddControl(ReleaseSelector, BorderLayoutManager.Position.North);
            layoutManager.AddControl(DropSelector, BorderLayoutManager.Position.West);
            layoutManager.AddControl(UseCaseCompletedControl, BorderLayoutManager.Position.Centre, true);

            // Connect the Drop and UseCases MultiSelector so that when an item is selected
            // in the Drops grid, the correct UseCases are shown in the child MultiSelector.
            

            DropSelector.GridSelector.Grid.BusinessObjectSelected += 
                delegate
                {
                    try
                    {
                        Drop selectedDrop = DropSelector.GridSelector.Grid.SelectedBusinessObject as Drop;
                        if (selectedDrop == null) return;
                        UseCaseCompletedControl.DropCompletedIn = selectedDrop;
                        UseCaseCompletedControl.Release = selectedDrop.Release;
                    }
                    catch (Exception ex)
                    {
                        GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
                    }
                };
        }

        /// <summary>
        /// Returns the collection of drops that is being shown in the <see cref="UseCaseManager.UI.DropCompletionControl.DropSelector"/>
        /// </summary>
        public BusinessObjectCollection<Drop> DropCol
        {
            get { return _dropCol; }
            set
            {
                _dropCol = value;
                DropSelector.GridSelector.BusinessObjectCollection=_dropCol;
            }
        }

        /// <summary>
        /// Called whenever the control is placed on a form by the menu system.  Put any initialisation
        /// code in here that must be run whenever the form is displayed.
        /// </summary>
        public void SetForm(IFormHabanero form)
        {
        }
    }
}